﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Syufy.BINLookup.Web.Helpers;
using Syufy.BINLookup.Web.Interfaces;
using Syufy.BINLookup.Web.Models;

namespace Syufy.BINLookup.Web.Controllers.Api
{
    /// <summary>
    /// Web API for BIN data.
    /// </summary>
    public class BinDataController : ApiController
    {
        private IUnitOfWork _unitOfWork;

        /// <summary>
        /// BIN data controller constructor.
        /// </summary>
        public BinDataController() : this(new UnitOfWork<BinEntities>())
        {
        }

        /// <summary>
        /// BIN data controller constructor (dependency injection).
        /// </summary>
        /// <param name="unitOfWork"></param>
        public BinDataController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        // GET: api/BinData
        /// <summary>
        /// Get all BIN data.
        /// </summary>
        /// <returns>List of BIN data.</returns>
        [ResponseType(typeof(IEnumerable<cardBINDB>))]
        public IHttpActionResult Get()
        {
            return Ok(_unitOfWork.GetRepository<cardBINDB>().Get());
        }

        // GET: api/BinData/5
        /// <summary>
        /// Get specific BIN data.
        /// </summary>
        /// <param name="id">BIN Number.</param>
        /// <returns>BIN data based on specified BIN number.</returns>
        [ResponseType(typeof(cardBINDB))]
        public IHttpActionResult Get(int id)
        {
            var cardBINDB = _unitOfWork.GetRepository<cardBINDB>().GetFirstOrDefault(x => x.cardBin.Equals(id.ToString()));

            if (cardBINDB == null)
            {
                return NotFound();
            }

            return Ok(cardBINDB);
        }

        // POST: api/BinData
        /// <summary>
        /// Create BIN lookup history record.
        /// </summary>
        /// <param name="history">Lookup history object.</param>
        /// <returns>URL for BIN Lookup History.</returns>
        [ResponseType(typeof(BINLookupHistory))]
        public async Task<IHttpActionResult> Post(BINLookupHistory history)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            history.CardType = history.CardType.ReturnDefaultIfNull(string.Empty);
            history.CreatedBy = history.CreatedBy.ReturnDefaultIfNull(User.Identity.Name);
            history.LastModifiedBy = history.LastModifiedBy.ReturnDefaultIfNull(User.Identity.Name);
            history.CreatedDate = history.CreatedDate.ReturnDefaultIfMinDate(DateTime.Now);
            history.LastModifiedDate = history.LastModifiedDate.ReturnDefaultIfMinDate(DateTime.Now);

            _unitOfWork.GetRepository<BINLookupHistory>().Insert(history);
            await _unitOfWork.SaveAsync();

            return CreatedAtRoute("DefaultApi", new { id = history.ID }, history);
        }

        /// <summary>
        /// IDisposable override.
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}